package com.xd.lib.tool;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.xd.lib.Const;
import com.xd.lib.R;
import com.xd.lib.listview.ListViewActivity;
import com.xd.lib.listview.horizontal.HvListViewActivity;
import com.xd.lib.tool.screenshot.ScreenShotUtil;
import com.xd.lib.tool.util.AppConfig;
import com.xd.lib.tool.util.FileUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/6/12.
 */
public class ToolActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_layout);

        int[] icons = {
                R.drawable.config,
                R.drawable.snipping
        };
        String[][] funcNames = {
                {"保存配置", "保存配置信息到应用程序的数据文件中"},
                {"屏幕截图", "截取当前屏幕，保存到内存卡中"}
        };

        List<Map<String, Object>> data = new ArrayList<>();
        for (int i=0;i<funcNames.length;i++){
            Map<String, Object> map = new HashMap<>();
            map.put(Const.STRING_ICON, icons[i]);
            map.put(Const.STRING_TITLE, funcNames[i][Const.INDEX_LISTVIEW_TITLE]);
            map.put(Const.STRING_DESCRIPTION, funcNames[i][Const.INDEX_LISTVIEW_DESCRIPTION]);
            data.add(map);
        }

        SimpleAdapter adapter = new SimpleAdapter(
                this,
                data,
                R.layout.listview_item,
                new String[]{Const.STRING_ICON, Const.STRING_TITLE, Const.STRING_DESCRIPTION},
                new int[]{R.id.listview_item_image, R.id.listview_item_txt_title, R.id.listview_item_txt_description}
        );

        final ListView listView = (ListView)findViewById(R.id.listview);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int index = listView.getCount() - position;
                Intent intent = null;
                switch (index){
                    case 2:
                        AppConfig config = new AppConfig();
                        config.setStageIndex(5);
                        config.setCoins(100);
                        FileUtil.saveConfig(ToolActivity.this, config);
                        Toast.makeText(ToolActivity.this, "保存配置成功！", Toast.LENGTH_SHORT).show();
                        config = FileUtil.loadConfig(ToolActivity.this);
                        Toast.makeText(ToolActivity.this, "读取配置成功！", Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        Bitmap bitmap = ScreenShotUtil.getShot(ToolActivity.this);
                        try {
                            // 需要设置权限
                            // <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
                            FileUtil.saveImage(bitmap, "picture", "shot.png");
                            Toast.makeText(ToolActivity.this, "图片已保存在picture/shot.png路径下！", Toast.LENGTH_SHORT).show();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        break;
                }
                if (intent != null) {
                    ToolActivity.this.startActivity(intent);
                }
            }
        });
    }
}
